package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.*;
import cn.gson.crm.model.mapper.CpMapper;
import cn.gson.crm.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class Cpservice {
    @Autowired
    CpMapper cpMapper;
    @Autowired
    CpDao cpDao;
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    CompanyDao companyDao;
    @Autowired
    CpxqDao cpxqDao;
    @Autowired
    PictureDao pictureDao;
    @Autowired
    SpecificationsDao specificationsDao;
    @Autowired
    SpecificationvalueDao specificationvalueDao;
//    public List<Pro> cp(){
//        return cpMapper.selectcp();
//    }


    //新增
    public void cpxz(Cpxz cpxz,String tp) {
        System.err.println("图片："+tp);
        Category category = cpxz.getFen();
        //新增产品主表
        Pro proList = cpxz.getProList();
        proList.setCategory(category);
        Pro save = cpDao.save(proList);

        //新增产品从表
        ProductXq proXQ = cpxz.getProXQ();
        proXQ.setPro(save);
        ProductXq productXq = cpxqDao.save(proXQ);

        System.out.println("产品id:"+save.getCpId());
        System.out.println("类别id:"+category.getCategoryId());
        System.out.println("详情id:"+productXq.getCpxqId());


        //规格表

        for(int i=0;i<cpxz.getGg().length;i++){
            System.out.println("长度:"+cpxz.getGg().length);
            Specifications specifications = new Specifications();
            specifications.setGgZt("0");
            specifications.setGgName(cpxz.getGg()[i]);
            specifications.setPro6(save);
            specificationsDao.save(specifications);

                SpecifValue specifValue = new SpecifValue();
                specifValue.setSpecifications(specifications);
                specifValue.setProductXq3(productXq);
                specifValue.setCategory3(category);
                specifValue.setGgzValue(cpxz.getGgz()[i]);
                specificationvalueDao.save(specifValue);


        }

        //图片表
        System.out.println("图片id："+cpxz.getTt().getPictureId());
        Picture picture = new Picture();
        if(cpxz.getTt().getPictureId()>0){
            picture.setProductXq1(productXq);
            picture.setPictureName(tp);
            picture.setPictureId(cpxz.getTt().getPictureId());
            pictureDao.save(picture);
        }else {
            picture.setProductXq1(productXq);
            picture.setPictureName(tp);
            pictureDao.save(picture);
        }



    }


    //分页查询
    public List<Pro> cx(String name,String fenlei){
        return cpMapper.cplb(name,fenlei);
    }
    //查询类别下拉框
    public List<Category> lb(){
        return cpMapper.lb();
    }

    //按类别查询规格
    public List<Pro> gg(String bb){
        return cpMapper.cpgg(bb);
    }

    //根据规格名id查询规格名
    public List<Specifications> guigeming(Integer guigeid,Integer guigezhiid){
        return cpMapper.guigeming(guigeid,guigezhiid);
    }
    //根据规格值id查询规格值
//    public List<Specifications> guigezhi(Integer guigezhiid){
//        return cpMapper.guigezhi(guigezhiid);
//    }

    public List<SpecifValue> ming(String cpming,String lbming){
        return cpMapper.ming(cpming,lbming);
    }

    public List<Purchasexq> cgmx(){
        return cpMapper.cgmx();
    }

    public List<ProductXq> xqId(Integer xqId){
        return cpMapper.tupian(xqId);
    }

    //修改类别
    @Transactional(transactionManager = "transactionManager")
    public void xg(Integer cpId){
        cpxqDao.xg(cpId);
    }

}
